
import { _decorator, Component, Node, Prefab, instantiate, director, Collider2D, Contact2DType, PhysicsSystem2D, ParticleSystem2D, IPhysics2DContact, RigidBody2D, Label } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = GameInt
 * DateTime = Mon Apr 18 2022 17:58:08 GMT+0800 (中国标准时间)
 * Author = xiaopang125
 * FileBasename = GameInt.ts
 * FileBasenameNoExtension = GameInt
 * URL = db://assets/GameInt.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('GameInt')
export class GameInt extends Component {
    // [1]
    dummy: Number = 0;

    // [2]
    // @property
    // serializableDummy = 0;
    _this = this;

    @property({ type: Prefab })
    public coinPrfb: Prefab | null = null;

    start() {
        // [3]
        this.schedule(function() {
            // 这里的 this 指向 component
           this.dummy++;
           console.log(this.dummy);

            this.creatCoin(2);
        }, 0.2);
        if (PhysicsSystem2D.instance) {
            PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
            PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
            PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        }
        this.node.on(Node.EventType.TOUCH_START, this.TOUCHCoin, this)
    }
    private TOUCHCoin() {
        this.creatCoin();
    }

    private creatCoin(n?, boxX?, boxY?) {
        

        const coin = instantiate(this.coinPrfb);
       
        if (boxX) {
            coin.setPosition(boxX, boxY);
        } else {
            coin.setPosition(Math.random() * 500, 500);
        }
        if (!n) { n = 2 };
        console.log(22);
        coin.getChildByName("Label").getComponent(Label).string = n;
        console.log(33);
        coin.parent = this.node;
        console.log(44);
        // label2.getComponent(Label).fontSize=10;

        // console.log(label2.getComponent(Label).string);


        // label2.getComponent(Label).string="99999";

        // coin.getComponent(RigidBody2D).enabledContactListener = true;
        let collider = coin.getComponent(Collider2D);
        collider.name = n;
        console.log(this.node);
        // if (collider) {
        //     collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        //     collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        //     collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
        //     collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        // }

        // this.coinArr.push(coin);
        // console.log(this.node);

    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        //    console.log(selfCollider);
        //    console.log(selfCollider.node);
        //     console.log(selfCollider.node.getChildByName("Label").getComponent(Label).string);
        // selfCollider.node.destroy();
        // selfCollider.node.getChildByName("Label").getComponent(Label).string="ppppp";

        // console.log('onBeginContact');






        // 只在两个碰撞体开始接触时被调用一次

    }
    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {

        // 只在两个碰撞体结束接触时被调用一次
        // console.log('onEndContact');
    }
    onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 每次将要处理碰撞体接触逻辑时被调用
        // console.log('onPreSolve');

    }
    onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 每次处理完碰撞体接触逻辑时被调用

        if (selfCollider.name && otherCollider.name) {

            // contact.disabled = true;
            if (selfCollider.name == otherCollider.name) {
                console.log('onPostSolve2');
                // setTimeout(() => {
                const n = Number(selfCollider.name) + 1;
                const nx=selfCollider.node.position.x;
                const ny=selfCollider.node.position.y;
                

                setTimeout(() => {
                    console.log(n);
                      this.creatCoin(n,nx,ny);
                }, 100);

             
               

                selfCollider.destroy();
                otherCollider.destroy();

                // }, 300,this);

            }
        }


    }

    update(deltaTime: number) {
        // [4]


    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
